﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;

namespace Zwms.Client.InfoWms
{
    public partial class GoodsCategoryForm : BaseForm
    {
        #region 成员变量
        public BaseGoodsCategory  CurrentGoodsCategory { get; set; }
        private EnumOpenType OpenType { get; set; }
        #endregion

        #region 构造函数
        public GoodsCategoryForm(EnumOpenType opentype, string sGoodsCategoryCode)
        {
            InitializeComponent();
            OpenType = opentype;
            InitBtnImage();
            if (opentype == EnumOpenType.New)
            {
                CurrentGoodsCategory = new BaseGoodsCategory();
            }
            else if (opentype == EnumOpenType.Open)
            {
                tbCategoryCode.ReadOnly = true;
                //通过服务读取'入库计划明细'的实例

                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                BaseGoodsCategory record = new BaseGoodsCategory();
                record.Id = sGoodsCategoryCode;
                CurrentGoodsCategory = client.QueryGoodsCategory(record);
            }

            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnConfirm.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnClear.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.exit;
            
        }
        private void InitControl()
        {
            //初始化父类别ComboBox
            BaseGoodsCategory record = new BaseGoodsCategory();
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            PagerIndex pageindex = new PagerIndex(1,int.MaxValue);
            List<BaseGoodsCategory> queryList = client.QueryGoodsCategoryList(record, ref pageindex).Where(c=>c.CategoryLevel ==1).ToList();
            foreach(BaseGoodsCategory queryrecord in queryList)
            {
                MyListItem item = new MyListItem(queryrecord.Id, queryrecord.CategoryName);
                cmbParentCode.Items.Add(item);
                cmbParentCode.DisplayMember = "Name";
                cmbParentCode.ValueMember = "ID";
            }

            //
            if (this.OpenType == EnumOpenType.New)
            {
                cmbCategoryLevel.SelectedIndex = 0;
                cmbParentCode.Enabled = false;
            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                LoadInfo(CurrentGoodsCategory);
            }
        }

        private void LoadInfo(BaseGoodsCategory record)
        {
            this.tbCategoryCode.Text = record.Id;
            this.tbCategoryName.Text = record.CategoryName;
            if (record.CategoryLevel == 1)
            {
                cmbCategoryLevel.SelectedIndex = 0;
            }
            else
            {
                cmbCategoryLevel.SelectedIndex = 1;
            }
            MyListItem item = new MyListItem(record.Id,record.CategoryName);

            this.cmbParentCode.SelectedIndex = this.cmbParentCode.Items.IndexOf(item);
            this.tbCreateUser.Text = record.CreateUserName;
            this.dpuCreateDT.SetDate(Helper.CommonHelper.GetDatetimeFromString(record.CreateDate + record.CreateTime));
            this.tbEditUser.Text = record.EditUserName;
            this.dpuEditDT.SetDate(Helper.CommonHelper.GetDatetimeFromString(record.EditDate + record.EditTime));

            
            this.tbRemark.Text = record.Remark;

           
        }
        #endregion

        #region 控件函数
        private void cmbCategoryLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbCategoryLevel.SelectedIndex == 0)
            {
                cmbParentCode.Enabled = false;
            }
            else
            {
                cmbParentCode.Enabled = true;  
            }

         
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (this.OpenType == EnumOpenType.New)
            {
                if (tbCategoryCode.Text.Trim() == "")
                {
                    bRet = false;
                    tbCategoryCode.BackColor = Color.Yellow;
                }
            }

            if (tbCategoryName.Text.Trim() == "")
            {
                bRet = false;
                tbCategoryName.BackColor = Color.Yellow;
            }

            if (cmbCategoryLevel.SelectedIndex == -1)
            {
                bRet = false;
                cmbCategoryLevel.BackColor = Color.Yellow;
            }

            if (!bRet)
            {
                return;
            }
            if (MessageBox.Show("您确定要添加（修改）货物分类吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                CurrentGoodsCategory.Id = tbCategoryCode.Text;
                CurrentGoodsCategory.CategoryName = tbCategoryName.Text;
                if (chbIsDeactived.Checked)
                {
                    CurrentGoodsCategory.IsDeactived = "1";
                }
                else
                {
                    CurrentGoodsCategory.IsDeactived = "0";
                }
                if (cmbCategoryLevel.SelectedIndex == 0)
                {
                    CurrentGoodsCategory.CategoryLevel = 1;
                }
                else
                {
                    CurrentGoodsCategory.CategoryLevel = 2;
                    CurrentGoodsCategory.ParetCode = ((MyListItem)cmbParentCode.SelectedItem).ID;
                }
                CurrentGoodsCategory.Remark = tbRemark.Text;
                //------------------------------------------------------
               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                if (this.OpenType == EnumOpenType.New)
                {
                    CurrentGoodsCategory.CreateUserName = MainForm.CurrentUser.UserName;
                    CurrentGoodsCategory.CreateUserCode = MainForm.CurrentUser.Id;
                    CurrentGoodsCategory.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                    CurrentGoodsCategory.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                    CurrentGoodsCategory.EditUserName = "";
                    CurrentGoodsCategory.EditUserCode = "";
                    CurrentGoodsCategory.EditDate = "";
                    CurrentGoodsCategory.EditTime = "";
                }
                else
                {
                    CurrentGoodsCategory.EditUserName = MainForm.CurrentUser.UserName;
                    CurrentGoodsCategory.EditUserCode = MainForm.CurrentUser.Id;
                    CurrentGoodsCategory.EditDate = Helper.CommonHelper.GetDateString(dtNow);
                    CurrentGoodsCategory.EditTime = Helper.CommonHelper.GetTimeString(dtNow);
                }
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                if (this.OpenType == EnumOpenType.New)
                {
                    bRet = client.AddGoodsCategory(CurrentGoodsCategory);
                }
                else
                {
                    bRet = client.UpdateGoodsCategory(CurrentGoodsCategory);
                }
                if (bRet)
                {
                    MessageBox.Show("成功!");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("未能添加，请检查编号是否已存在！");
                    return;
                }


            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (this.OpenType == EnumOpenType.New)
            {
                tbCategoryCode.Text = "";
            }
            tbCategoryName.Text = "";
            tbRemark.Text = "";
            cmbCategoryLevel.SelectedIndex = -1;
            cmbParentCode.SelectedIndex = -1;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void Control_Enter(object sender, EventArgs e)
        {
            this.Control_Enter(sender);
        }

        private void Control_Leave(object sender, EventArgs e)
        {
            this.Control_Leave(sender);
        }
        #endregion
    }
}
